package myApp.KnorrAndroid.controller;

import myApp.Constant.ActionEventConstant;
import myApp.KnorrAndroid.common.ActionEvent;
import myApp.KnorrAndroid.common.ModelEvent;
import myApp.KnorrAndroid.lib.network.http.HTTPRequest;
import myApp.KnorrAndroid.model.UserModel;
import myApp.KnorrAndroid.ultils.KnorrLog;
import myApp.KnorrAndroid.view.KnorrBaseActivity;
import myApp.KnorrAndroid.view.member.LoginView;
import myApp.KnorrAndroid.view.member.RegisterView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class UserController extends AbstractController {
	static UserController instance;

	protected UserController() {
	}

	public static UserController getInstance() {
		if (instance == null) {
			instance = new UserController();
		}
		return instance;
	}
	
	@Override
	public void handleSwitchActivity(ActionEvent e) {
		Activity base = (Activity) e.sender;
		Intent intent;
		Bundle extras;
		switch(e.action){
		case ActionEventConstant.GO_TO_LOGIN_VIEW:{
			intent = new Intent(base, LoginView.class);
			extras = (Bundle) e.viewData;
			intent.putExtras(extras);
			base.startActivity(intent);
			break;
		}
		case ActionEventConstant.GO_TO_REGISTER_VIEW:{
			intent = new Intent(base, RegisterView.class);
			extras = (Bundle) e.viewData;
			intent.putExtras(extras);
			base.startActivity(intent);
			break;
		}
		}
	}
	
	
	@Override
	public void handleViewEvent(ActionEvent e) {
		HTTPRequest request = null;
		boolean isBlockRequest = false;
		KnorrBaseActivity baseActivity = (KnorrBaseActivity)e.sender;
		switch (e.action) {
		case ActionEventConstant.LOGIN:{
			request = UserModel.getInstance().requestLoginHTTP(e);
			isBlockRequest = true;
			break;
		}
		case ActionEventConstant.REGISTER:{
			request = UserModel.getInstance().requestRegister(e);
			isBlockRequest = true;
			break;
		}
		}
		//add request to view to cancel
		if (request != null && baseActivity != null) {
			baseActivity.addProcessingRequest(request, isBlockRequest);
			e.isBlockRequest = isBlockRequest;
			e.request = request;
		}
	}

	@Override
	public void handleModelEvent(final ModelEvent modelEvent) {
		final ActionEvent e = modelEvent.getActionEvent();
		KnorrBaseActivity ac = (KnorrBaseActivity) e.sender;
		HTTPRequest request = e.request;
		
		if (ac.isFinished) {
			return;
		}
		
		ac.runOnUiThread(new Runnable() {
			public void run() {
				// TODO Auto-generated method stub
				KnorrBaseActivity base = (KnorrBaseActivity) e.sender;
				base.handleModelViewEvent(modelEvent);
			}
		});
		if (request != null) {
			ac.removeProcessingRequest(request, e.isBlockRequest);
			e.request = null;
		}

	}

	@Override
	public void handleErrorModelEvent(final ModelEvent modelEvent) {
		// TODO Auto-generated method stub
		handleCommonError(modelEvent);
		final ActionEvent e = modelEvent.getActionEvent();
		KnorrBaseActivity ac = (KnorrBaseActivity) e.sender;
		HTTPRequest request = e.request;
		ac.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				KnorrLog.e("Error", "handleErrorModelEvent deletecontact");
				KnorrBaseActivity base = (KnorrBaseActivity) e.sender;
				base.handleErrorModelViewEvent(modelEvent);
			}
		});
		if (request != null) {
			ac.removeProcessingRequest(request, e.isBlockRequest);
			e.request = null;
		}
	}
	

}
